package com.ton.utils.tron.http;

import cn.hutool.json.JSONUtil;
import com.ton.utils.http.HttpUtils;
import com.ton.utils.http.RestTemplateUtils;
import com.ton.utils.tron.http.feee.ApiKeyDetails;
import com.ton.utils.tron.http.feee.BuyOrderDetail;
import com.ton.utils.tron.http.feee.FeeeApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class FeeeAPI extends HttpUtils {
    public static String get(String url, Map<String, Object> params) {
        String baseUrl = "https://feee.io/open";
        Map<String, String> headers = new HashMap<>();
        headers.put("key", "7bbec29f-0c9f-4908-b0d4-335a1acec813");
        headers.put("User-Agent", "Feee.io Client/1.0.0 (https://feee.io)");
        headers.put("Accept", "*/*");
        return HttpUtils.get(baseUrl + url, headers, params);
    }

    ///v2/order/submit
    public static String post(String url, Map<String, Object> params) {
        Map<String, String> headers = new HashMap<>();
        String baseUrl = "https://feee.io/open";
        headers.put("Content-Type", "application/json;charset=utf-8");
        headers.put("key", "7bbec29f-0c9f-4908-b0d4-335a1acec813");
        headers.put("User-Agent", "Feee.io Client/1.0.0 (https://feee.io)");
        headers.put("Accept", "*/*");
        return HttpUtils.post(url, headers, params);
    }


    /**
     * 能量租赁API
     *
     * @param key            Feee.io 官网申请的Key
     * @param receiveAddress 能量接收的地址
     * @param num            租赁的数量
     * @param time           租赁的事件
     * @return 返回JSON
     * @throws IOException 抛出IO异常
     */
    public static BuyOrderDetail energyEent(String key, String receiveAddress, Long num, Integer time) {
        String unit = "h";
        Integer submitTime = time;
        if (time >= 24) {
            unit = "d";
            submitTime = Integer.parseInt(String.valueOf(time / 24));
        }


        Map<String, Object> map = new HashMap<>();
        map.put("resource_type", 1);
        map.put("receive_address", receiveAddress);
        map.put("resource_value", num);
        map.put("rent_duration", submitTime); //租用时长，当时间单位等于 h，暂时只支持 1 和 3
        map.put("rent_time_unit", unit); //租用时间单位，仅支持 d 天、h 小时

        Map<String, String> header = new HashMap<>();
        header.put("key", key);
        header.put("User-Agent", "Feee.io Client/1.0.0 (https://feee.io)");
        header.put("Content-Type", "application/json");
        String doPost = RestTemplateUtils.doPost("https://feee.io/open/v2/order/submit", header, map);
        if (doPost.isEmpty()) {
            log.error("发送POST请求返回为空");
            return new BuyOrderDetail();
        }
        FeeeApiResult result = JSONUtil.toBean(doPost, FeeeApiResult.class);
        String data = result.getData();
        System.out.println(data);
        // return FeeeAPI.post("https://feee.io/open/v2/order/submit", map);
        return JSONUtil.toBean(data, BuyOrderDetail.class);

    }

    public static ApiKeyDetails getApiKeyDetails(String key) {
        HashMap<String, String> headers = new HashMap<>();
        headers.put("key", key);
        headers.put("User-Agent", "Feee.io Client/1.0.0 (https://feee.io)");
        String json = RestTemplateUtils.doGet("https://feee.io/open/v2/api/query", headers, new HashMap<>());
        FeeeApiResult result = JSONUtil.toBean(json, FeeeApiResult.class);
        String data = result.getData();
        return JSONUtil.toBean(data, ApiKeyDetails.class);
    }
}
